﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Google.GData.Client;
using Google.GData.Photos;
using Google.Picasa;

namespace GoogleServices.Picasa
{
    public class PicasaServiceWraper
    {
        private readonly PicasaService service;

        private RequestSettings setting;

        private readonly string appName;

        private string user;

        public PicasaServiceWraper(string appName)
        {
            this.appName = appName;
            service=new PicasaService(appName);
        }

        public void LogintoService(string user,string pass)
        {
            this.user = user;
           service.setUserCredentials(user,pass);
           setting=new RequestSettings(appName,user,pass);
        }

        #region album

        public PicasaFeed SearchAlbum()
        {
            var query=new AlbumQuery(PicasaQuery.CreatePicasaUri(service.Credentials.Username));
            return service.Query(query);
        }

        public PicasaEntry AddAlbum(string albumTitle,string albumSummary,string albumAccess)
        {
            var newEntry=new AlbumEntry {Title = {Text = albumTitle}, Summary = {Text = albumSummary}};

            var feedUrl =new Uri( PicasaQuery.CreatePicasaUri(service.Credentials.Username));

            return service.Insert(feedUrl, newEntry);
        }

        public PicasaEntry UpdateAlbum(PicasaEntry entry)
        {
            return (PicasaEntry)entry.Update();
        }

        public void DeleteAlbum(PicasaEntry entry)
        {
            entry.Delete();
        }

        public Feed<Album> GetAlbums()
        {
            var request=new PicasaRequest(setting);
            return request.GetAlbums();
        }

        #endregion

        #region Photo

        public  PicasaFeed ListPhotoInAlbum(string albumId)
        {
            var query = new PhotoQuery(PicasaQuery.CreatePicasaUri(service.Credentials.Username, albumId));

            return service.Query(query);
        }

        public PicasaFeed ListLastPhoto(int count)
        {
            var query = new PhotoQuery(PicasaQuery.CreatePicasaUri(service.Credentials.Username)) {NumberToRetrieve = 10};

            return service.Query(query);
        }

        public PicasaEntry AddNewPhoto(string albumId,Stream contentStream,string contentType)
        {
            var postUri=new Uri(PicasaQuery.CreatePicasaUri(service.Credentials.Username,albumId));

            var createdEntry = (PicasaEntry)service.Insert(postUri, contentStream, contentType, string.Empty);

            contentStream.Close();
            contentStream.Dispose();

            return createdEntry;
        }

        public PicasaEntry UpdatePhoto(PicasaEntry entry)
        {
            return (PicasaEntry)entry.Update();
        }

        public void DeletePhoto(PicasaEntry entry)
        {
            entry.Delete();
        }

        public Feed<Photo> GetPhotos()
        {
            var request=new PicasaRequest(setting);
            return request.GetPhotos();
        }

        public Stream DownloadPhoto(Photo photo)
        {
            var request = new PicasaRequest(setting);
            return request.Download(photo);
        }

        #endregion
    }
}
